package com.example;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import com.google.protobuf.ByteString;
import com.parsifal.serializationdemo.bean.LatLngProto.LatLng;
import com.parsifal.serializationdemo.bean.PoiInfoProto.PoiInfo;

public class MyClass {

    public static void main(String[] args) {
        // 简单类型
        try {
            LatLng locationToFile = LatLng.newBuilder()
                    .setLongitude(121.5393720000d)
                    .setLatitude(38.8684930000d)
                    .build();
            OutputStream outputStream = new FileOutputStream("location.txt");
            locationToFile.writeTo(outputStream);
            outputStream.close();

            InputStream inputStream = new FileInputStream("location.txt");
            LatLng locationFromFile = LatLng.parseFrom(inputStream);
            inputStream.close();
            System.out.println(locationFromFile.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 复杂类型
        try {
            PoiInfo poiInfoToFile = PoiInfo.newBuilder()
                    .setAddress(ByteString.copyFromUtf8("高新园区黄浦路"))
                    .setCity(ByteString.copyFromUtf8("大连"))
                    .setLocation(LatLng.newBuilder()
                            .setLongitude(121.5393720000d)
                            .setLatitude(38.8684930000d)
                            .build())
                    .setName(ByteString.copyFromUtf8("万达广场"))
                    .setPhoneNum(ByteString.copyFromUtf8("(0411)39983777"))
                    .setPostCode(ByteString.copyFromUtf8("116000"))
                    .setType(PoiInfo.POITYPE.POINT)
                    .build();
            OutputStream outputStream = new FileOutputStream("poi.txt");
            poiInfoToFile.writeTo(outputStream);
            outputStream.close();

            InputStream inputStream = new FileInputStream("poi.txt");
            PoiInfo poiInfoFromFile = PoiInfo.parseFrom(inputStream);
            inputStream.close();
            System.out.println(poiInfoFromFile.toString());
            System.out.println(poiInfoFromFile.getName().toStringUtf8());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
